package com.cloud.bc.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcSetmealOrders;
import com.cloud.bc.model.entity.BcSetmeals;
import com.cloud.bc.service.BcSetmealOrdersService;
import com.cloud.bc.service.BcSetmealsService;
import com.cloud.bc.util.OrderNoUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("m/center/setmeal")
@AllArgsConstructor
public class MCenterSetmealController extends  BaseController{
    private final BcSetmealsService bcSetmealsService;
    private final OrderNoUtil orderNoUtil;
    private final BcSetmealOrdersService bcSetmealOrdersService;

    /**
     * 套餐
     */
    @GetMapping("")
    public ModelAndView setmeal(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"套餐", "m/center/setmeal/index");
        BcSetmeals bcSetmeals = new BcSetmeals();
        bcSetmeals.setDisplay(1);
        List<BcSetmeals> bcSetmealsList = bcSetmealsService.selectAll(bcSetmeals);
        view.addObject("bcSetmealsList", bcSetmealsList);
        return view;
    }

    /**
     * 套餐
     */
    @GetMapping("pay")
    public void setmealPay(HttpServletRequest request, HttpServletResponse response, Integer setmeal_id)
            throws IOException {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSetmeals byId = bcSetmealsService.getById(setmeal_id);
        //TODO 创建一个待支付订单
        BcSetmealOrders bcSetmealOrders = new BcSetmealOrders();
        BeanUtil.copyProperties(byId, bcSetmealOrders);
        bcSetmealOrders.setUserId(userInfo.getId());
        bcSetmealOrders.setSetmealId(setmeal_id);
        bcSetmealOrders.setSetmealName(byId.getName());
        String orderId = orderNoUtil.getSetmeals();
        bcSetmealOrders.setOrderId(orderId);
        bcSetmealOrdersService.save(bcSetmealOrders);
        response.sendRedirect("/m/pay/pay/" + orderId);
        //TODO跳转到收银台
    }
}
